﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Hdt.TransOcean.Dal ;
using System.Security.Cryptography;
using System.Data;
using System.Collections.Generic;

namespace Hdt.TransOcean
{
    class Common
    {
        public static void ShowMessage(string message, string capption, EOperation operation)
        {
            string strOpt = string.Empty;
            switch(operation)
            {
                case EOperation.New:
                    strOpt = " - " + "Thêm";
                    break;
                case EOperation.Delete:
                    strOpt = " - " + "Xóa";
                    break;
                case EOperation.Save:
                    strOpt = " - " + "Lưu";
                    break;
                case EOperation.Close:
                    strOpt = " - " + "Đóng";
                    break;
                case EOperation.Edit:
                    strOpt = " - " + "Sửa";
                    break;
                case EOperation.Import:
                    strOpt = " - " + "Lưu vào Excel";
                    break;
                case EOperation.Export:
                    strOpt = " - " + "Nhập từ Excel";
                    break;            
            }
            capption += strOpt;
            MessageBox.Show(message, capption);
        }

        public static string AutoCode(string strPrefix, string strNumber)
        {
            //AutoCode('HD','0001')= 'HD0002'
            string result;
            string format = "00000000000000000000000";

            format = format.Substring(0, strNumber.Length);
            result = strPrefix + String.Format("{0:" + format + "}", (Int32.Parse(strNumber) + 1));
            return result;
        }

        public static string GetMd5Hash(string input)
        {
            MD5 md5Hash = MD5.Create();
            // Convert the input string to a byte array and compute the hash. 
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

            // Create a new Stringbuilder to collect the bytes 
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data  
            // and format each one as a hexadecimal string. 
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string. 
            return sBuilder.ToString();
        }


        public static Object IsNull(Object obj)
        {
            Object str = string.Empty;
            //if (obj == null) 
            //{
            //    str = "";
            //}
            //else
            //{
            //    str = obj;
            //}
            str = obj == null ? str : obj;
            return str;
        }

        public static object IIF(bool cond, object left, object right)
        {
            return cond ? left : right;
        }

        public static string Left(string param, int length)
        {
            //we start at 0 since we want to get the characters starting from the
            //left and with the specified lenght and assign it to a variable
            string result = param.Substring(0, length);
            //return the result of the operation
            return result;
        }

        public static string Right(string param, int length)
        {
            //start at the index based on the lenght of the sting minus
            //the specified lenght and assign it a variable
            string result = param.Substring(param.Length - length, length);
            //return the result of the operation
            return result;
        }

        public static string Mid(string param, int startIndex, int length)
        {
            //start at the specified index in the string ang get N number of
            //characters depending on the lenght and assign it to a variable
            string result = param.Substring(startIndex, length);
            //return the result of the operation
            return result;
        }

        public static string Mid(string param, int startIndex)
        {
            //start at the specified index and return all characters after it
            //and assign it to a variable
            string result = param.Substring(startIndex);
            //return the result of the operation
            return result;
        }

        public void SetValueToTableUpdateString(string tableName)
        {
            foreach (Type t in GetType().Assembly.GetTypes())
                {
                    if (t.IsSubclassOf(typeof(Form)))
                    {
                        // and new value
                        if (GlobalVariable.strTableUpdate.ToString().Contains(t.Name + "." + tableName) == false)
                        {
                            GlobalVariable.strTableUpdate.Append(t.Name + "." + tableName + ",");
                        }
                    }
                }
        }

        public void RemoveValueInTableUpdateString(string formName, string tableName)
        {
            if (GlobalVariable.strTableUpdate.ToString().Contains(formName + "." + tableName) == true)
            {
                GlobalVariable.strTableUpdate = GlobalVariable.strTableUpdate.Replace(formName + "." + tableName, String.Empty);
            }
        }

        public void RemoveAllWithTabelNameValueInTableUpdateString(string formName)
        {
            GlobalVariable.strTableUpdate = GlobalVariable.strTableUpdate.Replace(formName + "." + EObject.Customer.ToString(), String.Empty);
            GlobalVariable.strTableUpdate = GlobalVariable.strTableUpdate.Replace(formName + "." + EObject.Journey.ToString(), String.Empty);
            GlobalVariable.strTableUpdate = GlobalVariable.strTableUpdate.Replace(formName + "." + EObject.Item.ToString(), String.Empty);
            GlobalVariable.strTableUpdate = GlobalVariable.strTableUpdate.Replace(formName + "." + EObject.Employee.ToString(), String.Empty);
            GlobalVariable.strTableUpdate = GlobalVariable.strTableUpdate.Replace(formName + "." + EObject.SaleInvoice.ToString(), String.Empty);
            GlobalVariable.strTableUpdate = GlobalVariable.strTableUpdate.Replace(formName + "." + EObject.ReturnInvoice.ToString(), String.Empty);
            GlobalVariable.strTableUpdate = GlobalVariable.strTableUpdate.Replace(formName + "." + EObject.Airline.ToString(), String.Empty);
            GlobalVariable.strTableUpdate = GlobalVariable.strTableUpdate.Replace(formName + "." + EObject.User.ToString(), String.Empty);
        }

        public Boolean CheckValueInTableUpdateString(string formName, string tableName)
        {
            Boolean flag = false;
            if (GlobalVariable.strTableUpdate.ToString().Contains(formName + "." + tableName) == true)
            {
                flag = true;
            }
            return flag;
        }

    }
}
